package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.MemberReport;
import com.itheima.health.entity.Result;
import com.itheima.health.entity.SetmealReport;
import com.itheima.health.job.ReportCaluJob;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.ReportService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.vo.BusinessReport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;


@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ReportCaluJob reportCaluJob;
    @Autowired
    private ReportService reportService;
    @Autowired
    private SetMealService setMealService;

    /**
     * 会员报表
     * @return
     */
    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        List<String> months =new ArrayList<>();
        List<Integer> memberCount =new ArrayList<>();

        Map<String,Integer> map = redisTemplate.boundHashOps("memberReport").entries();
        if(map == null || map.size() <= 0){
            log.info("第一次执行了手动查询!!!!!!");
            reportCaluJob.memberCountBefore12Month();
            reportCaluJob.memberCountByMonth();
            map = redisTemplate.boundHashOps("memberReport").entries();
        }
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            months.add(entry.getKey());
            memberCount.add(entry.getValue());
        }
        Collections.sort(months);
        Collections.sort(memberCount);

        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,new MemberReport(months,memberCount));
    }

    /**
     * 运营报表查询
     * @return
     */
    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        BusinessReport businessReport =reportService.getBusinessReportData();
        return new Result(true,MessageConst.GET_BUSINESS_REPORT_SUCCESS,businessReport);
    }
    /**
     * 运营报表下载
     */
    @GetMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletResponse response) throws Exception {
        reportService.exportBusinessReport(response);
    }

    /**
     * 套餐占比饼形图
     * @return
     */
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport(){
        //创建SetmealReport对象
        SetmealReport setmealReport = new SetmealReport();
        //创建findSetmealCount()方法  获取每个套餐的预约数以及套餐名称
        List<Map<String,Object>> setmealCount = setMealService.findSetmealCount();
        //封装到SetmealReport对象中
        setmealReport.setSetmealCount(setmealCount);
        //查看setmealCount是什么
        log.info("setmealCount{}",setmealCount);
        //创建list集合用来存放每个套餐的名称
        List<String> setmealNames = new ArrayList<>();
        //遍历 根据获取到的每个套餐的预约数以及套餐名称 把名称提取出来
        for (Map<String, Object> m : setmealCount) {
            log.info("m{}",m);
            //获取每个套餐的名称
            String name = m.get("name").toString();
            setmealNames.add(name);
        }
        setmealReport.setSetmealNames(setmealNames);

        return new Result(true,"套餐数据查询成功",setmealReport);
    }
}
